package com.saifan.wyy_ov.common;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;


import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.PermissionsBean;
import com.saifan.wyy_ov.data.bean.RoomInfoBean;
import com.saifan.wyy_ov.utils.ActivitysManager;

import java.util.List;

import lomasky.ma.httpUtils.JsonUtil;
import lomasky.ma.utils.SharedPrefUtil;


/**
 * @author Eicky
 * @Description: Activity基类
 * @date: 2016/4/23 13:19
 * @version: V1.0
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected Toolbar mToolbar;
    protected LinearLayout mRootLayout;
    protected AppCompatActivity mActivity;

    public List<PermissionsBean> getPermissions(){
        String per = SharedPrefUtil.getString(BaseActivity.this, "permissions", "");
        List<PermissionsBean> permissionsBeans = JsonUtil.getList(per, PermissionsBean.class);
        return permissionsBeans;
    }

    public List<RoomInfoBean> getRoomInfoBeanList() {
        if (roomInfoBeanList == null) {
            roomInfoBeanList = JsonUtil.getList(SharedPrefUtil.getString(this, "roomInfos", ""), RoomInfoBean.class);
        }
        return roomInfoBeanList;
    }

    public void setRoomInfoBeanList(List<RoomInfoBean> roomInfoBeanList) {
        this.roomInfoBeanList = roomInfoBeanList;
    }

    List<RoomInfoBean> roomInfoBeanList;

    public String getUserName() {
        if (userName == null) {
            userName = SharedPrefUtil.getString(this, "userName", "");
        }
        return userName;
    }

    String userName;

    public RoomInfoBean getRoomInfoBean() {
        if (roomInfoBean == null) {
            roomInfoBean = JsonUtil.getObject(SharedPrefUtil.getString(this, "roomInfo", ""), RoomInfoBean.class);
        }
        return roomInfoBean;

    }

    private Toolbar toolbar;

    public void setRoomInfoBean(RoomInfoBean roomInfoBean) {

        SharedPrefUtil.putString(this, "roomInfo", JsonUtil.toJson(roomInfoBean));
        this.roomInfoBean = roomInfoBean;

    }

    protected RoomInfoBean roomInfoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        // 这句很关键，注意是调用父类的方法
        super.setContentView(R.layout.activity_base);
        this.mRootLayout = (LinearLayout) findViewById(R.id.root_layout);
        initToolbar();
        ActivitysManager.getManager().addActivity(this);

    }

    private void initToolbar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
        }
    }


    @Override
    public void setContentView(int layoutId) {
        setContentView(View.inflate(this, layoutId, null));
    }

    @Override
    public void setContentView(View view) {
        if (mRootLayout == null) return;
        mRootLayout.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        initToolbar();
    }

    public void setEnabledNavigation(boolean isEnable) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(isEnable);

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void startActivity(Class clazz) {
        startActivity(new Intent(this, clazz));
    }
    public  void showToast(String msg) {

        Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
    }

    public   void showDialog( String msg ){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

            }
        });
        builder.setTitle("提示");
        builder.setMessage(msg);
        builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_BACK:
                        dialog.dismiss();

                        break;
                    default:
                        break;
                }
                return true;
            }
        });

        builder.setCancelable(false);
        builder.create().show();
    }
    public   void showDialog( String msg ,DialogInterface.OnClickListener onClickListener){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setNegativeButton("确定", onClickListener);
        builder.setTitle("提示");
        builder.setMessage(msg);
        builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_BACK:
                        dialog.dismiss();

                        break;
                    default:
                        break;
                }
                return true;
            }
        });

        builder.setCancelable(false);
        builder.create().show();
    }
}
